package com.meihua.shellapp.activity.personalcenter;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.meihua.shellapp.R;
import com.meihua.shellapp.base.BaseActivity;
import com.meihua.shellapp.base.BaseBean;
import com.meihua.shellapp.base.MyApplication;
import com.meihua.shellapp.bean.PersonalBean;
import com.meihua.shellapp.bean.PersonalDataBean;
import com.meihua.shellapp.databean.AreaDataBean;
import com.meihua.shellapp.dialog.LinkageDialog;
import com.meihua.shellapp.dialog.SlideListDialog;
import com.meihua.shellapp.utils.MyStringCallBack;
import com.meihua.shellapp.utils.ToastUtils;
import com.meihua.shellapp.utils.URL;
import com.meihua.shellapp.view.MyOnclickEditText;

import butterknife.Bind;

/**
 * 作者：lc on 2017/9/27.
 * 邮箱：lc0607live@163.com
 */

public class PersonalInfoActivity extends BaseActivity {

    @Bind(R.id.iv_top_left)
    ImageView iv_top_left;//返回
    @Bind(R.id.tv_title)
    TextView tv_title;//标题
    @Bind(R.id.iv_top_right)
    ImageView iv_top_right;//编辑
    @Bind(R.id.tv_top_right)
    TextView tv_top_right;//保存

    @Bind(R.id.et_name)
    MyOnclickEditText et_name;//姓名
    @Bind(R.id.et_gender)
    MyOnclickEditText et_gender;//性别
    @Bind(R.id.et_identity_card)
    MyOnclickEditText et_identity_card;//身份证号
    @Bind(R.id.et_education)
    MyOnclickEditText et_education;//学历
    @Bind(R.id.et_member_num)
    MyOnclickEditText et_member_num;//资格证号
    @Bind(R.id.et_email)
    MyOnclickEditText et_email;//邮箱
    @Bind(R.id.et_postcode)
    MyOnclickEditText et_postcode;//邮编
    @Bind(R.id.et_bank_user)
    MyOnclickEditText et_bank_user;//开户人姓名
    @Bind(R.id.et_bank_name)
    MyOnclickEditText et_bank_name;//开户行
    @Bind(R.id.et_bank_card)
    MyOnclickEditText et_bank_card;//帐号
    @Bind(R.id.rl_address)
    RelativeLayout rl_address;//联系地址
    @Bind(R.id.et_addr)
    MyOnclickEditText et_addr;//联系地址
    @Bind(R.id.et_address)
    MyOnclickEditText et_address;//详细地址
    @Bind(R.id.tv_name_member_id)
    TextView tv_name_member_id;//推荐人姓名
    @Bind(R.id.tv_recommend_member_id)
    TextView tv_recommend_member_id;//推荐人编号

    private PersonalDataBean data;


    @Override
    public int getLayoutId() {
        return R.layout.activity_personal_info;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        iv_top_left.setVisibility(View.VISIBLE);
        iv_top_left.setOnClickListener(this);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("个人信息");
        et_gender.setEnabled(false);
        et_education.setEnabled(false);
        iv_top_right.setVisibility(View.VISIBLE);
        iv_top_right.setImageResource(R.drawable.bianji);
        iv_top_right.setOnClickListener(this);
        tv_top_right.setText("保存");
        tv_top_right.setOnClickListener(this);
        et_addr.setOnClickListener(this);
        et_gender.setOnClickListener(this);
        et_education.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        hiddenInputMethod();
        super.onClick(view);
        switch (view.getId()) {
            case R.id.et_education://学历
                new SlideListDialog(mContext, new String[]{"高中", "大专", "本科", "硕士", "博士"}) {
                    @Override
                    public void sureData(String value, int selectIndex) {
                        data.setEducation(value);
                        et_education.setText(value);
                    }
                }.show();
                break;
            case R.id.et_gender://性别
                new SlideListDialog(mContext, new String[]{"女", "男"}) {
                    @Override
                    public void sureData(String value, int selectIndex) {
                        data.setGender(selectIndex + "");
                        et_gender.setText(value);
                    }
                }.show();
                break;
            case R.id.et_addr://选择联系地址
                new LinkageDialog(mContext) {
                    @Override
                    public void getArea(AreaDataBean province, AreaDataBean city, AreaDataBean county) {
                        data.setProvinces(province.getName());
                        data.setCitys(city.getName());
                        data.setDistricts(county.getName());
                        data.setProvince(province.getId());
                        data.setCity(city.getId());
                        data.setDistrict(county.getId());
                        et_addr.setText(province.getName() + "  " + city.getName() + "  " + county.getName());//联系地址
//                        ToastUtils.getInstance().showToast(province.getName() + "-" + city.getName() + "-" + county.getName());
                    }
                }.show();
                break;
            case R.id.tv_top_right://保存
                updateInfo();
                break;
            case R.id.iv_top_right://编辑
                changeViewStatus(1);
                break;
        }
    }

    /**
     * 修改显示格式
     *
     * @param type 1.可编辑状态   2.不可编辑状态
     */
    private void changeViewStatus(int type) {
        if (type == 1) {
            rl_address.setVisibility(View.VISIBLE);
            iv_top_right.setVisibility(View.GONE);
            tv_top_right.setVisibility(View.VISIBLE);
            et_gender.setEnabled(true);
            et_education.setEnabled(true);
            et_name.setFocusableAndGravity(true, Gravity.LEFT);//姓名
            et_gender.setFocusableAndGravity(false, Gravity.LEFT);//性别
            et_identity_card.setFocusableAndGravity(true, Gravity.LEFT);//身份证号
            et_education.setFocusableAndGravity(false, Gravity.LEFT);//学历
            et_member_num.setFocusableAndGravity(true, Gravity.LEFT);//资格证号
            et_email.setFocusableAndGravity(true, Gravity.LEFT);//邮箱
            et_postcode.setFocusableAndGravity(true, Gravity.LEFT);//邮箱
            et_bank_user.setFocusableAndGravity(true, Gravity.LEFT);//开户人姓名
            et_bank_name.setFocusableAndGravity(true, Gravity.LEFT);//开户行
            et_bank_card.setFocusableAndGravity(true, Gravity.LEFT);//帐号
            et_addr.setFocusableAndGravity(false, Gravity.LEFT);//联系地址
            et_address.setFocusableAndGravity(true, Gravity.LEFT);//详细地址
            et_address.setText(data.getAddress());//详细地址
        } else {
            rl_address.setVisibility(View.GONE);
            iv_top_right.setVisibility(View.VISIBLE);
            tv_top_right.setVisibility(View.GONE);
            et_gender.setEnabled(false);
            et_education.setEnabled(false);
            et_name.setFocusableAndGravity(false, Gravity.RIGHT);//姓名
            et_gender.setFocusableAndGravity(false, Gravity.RIGHT);//性别
            et_identity_card.setFocusableAndGravity(false, Gravity.RIGHT);//身份证号
            et_education.setFocusableAndGravity(false, Gravity.RIGHT);//学历
            et_member_num.setFocusableAndGravity(false, Gravity.RIGHT);//资格证号
            et_email.setFocusableAndGravity(false, Gravity.RIGHT);//邮箱
            et_postcode.setFocusableAndGravity(false, Gravity.RIGHT);//邮箱
            et_bank_user.setFocusableAndGravity(false, Gravity.RIGHT);//开户人姓名
            et_bank_name.setFocusableAndGravity(false, Gravity.RIGHT);//开户行
            et_bank_card.setFocusableAndGravity(false, Gravity.RIGHT);//帐号
            et_addr.setFocusableAndGravity(false, Gravity.RIGHT);//联系地址
            et_address.setFocusableAndGravity(false, Gravity.RIGHT);//详细地址
            et_address.setText(data.getAddr());//详细地址
        }
    }

    @Override
    public void createLoadData() {
        super.createLoadData();
        OkGo.<String>post(URL.getUrl().GETPERSONALINFO)
                .tag(this)
                .params("mid", MyApplication.getInstance().getMId())
                .execute(new MyStringCallBack<PersonalBean>(null) {

                    @Override
                    public void onSuccessData(PersonalBean bean) {
                        super.onSuccessData(bean);
                        if (bean.getData() != null) {
                            data = bean.getData();
                            bindData();
                        }
                    }
                });
    }

    /**
     * 绑定数据
     */
    private void bindData() {
        et_name.setText(data.getName());
        et_gender.setText(data.getGender().equals("1") ? "男" : "女");//性别
        et_identity_card.setText(data.getIdentity_card());//身份证号
        et_education.setText(data.getEducation());//学历
        et_member_num.setText(data.getMember_num());//资格证号
        et_email.setText(data.getEmail());//邮箱
        et_postcode.setText(data.getPostcode());//邮编
//        et_bank_user.setText(data.getBank_user());//开户人姓名
//        et_bank_name.setText(data.getBank_name());//开户行
//        et_bank_card.setText(data.getBank_card());//帐号
        et_addr.setText(data.getProvinces() + "  " + data.getCitys() + "  " + data.getDistricts());//联系地址
        et_address.setText(data.getAddr());//详细地址
        tv_name_member_id.setText(data.getName_member_id());
        tv_recommend_member_id.setText(data.getRecommend_member_id());
    }

    /**
     * 保存接口
     */
    private void updateInfo() {
        OkGo.<String>post(URL.getUrl().UPDATEPERSONALINFO)
                .params("m_number", data.getM_number())
                .params("name", et_name.getText().toString().trim())
                .params("member_num", et_member_num.getText().toString().trim())
                .params("gender", data.getGender())
                .params("identity_card", et_identity_card.getText().toString().trim())
                .params("email", et_email.getText().toString().trim())
                .params("postcode", et_postcode.getText().toString().trim())
                .params("education", data.getEducation())
//                .params("bank_name", et_bank_name.getText().toString().trim())
//                .params("bank_user", et_bank_user.getText().toString().trim())
//                .params("bank_card", et_bank_card.getText().toString().trim())
                .params("address", et_address.getText().toString().trim())
                .params("province", data.getProvince())
                .params("city", data.getCity())
                .params("district", data.getDistrict())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        BaseBean bean = MyApplication.getInstance().getBeanForJson(BaseBean.class, response.body());
                        if (bean == null) {
                            return;
                        }
                        if (bean.getCode() == 1) {
                            MyApplication.getInstance().bean.setName(et_name.getText().toString().trim());
                            data.setAddr(data.getProvinces() + data.getCitys() + data.getDistricts() + data.getAddress());
                            changeViewStatus(2);
                        } else {
                            ToastUtils.getInstance().showToast(bean.getMsg());
                        }
                    }
                });
    }
}
